package cringer::tinysong;

use JSON;

# tinysong.pm
# provides search functionality for cringer using the TinySong music search

our $VERSION = '2.0';

sub said{
 my $self = $_[0];
 my $args = $_[1];
 my $me = $self->{nick};
 
 unless($args->{body} =~ /^music: ?(.*)$/){ # if this isn't a search query, we skip it.
  return;
 }

 my @r; # set up the response array
 my $response;
 my $request = $1;
 $request =~ s/ $//;
 my $url, $string, $songinfo;
 $url = URI->new( "http://www.tinysong.com/s/$request" );
 $url->query_form( 'limit' => 5, 'format' => json );
 $string ='';
 $songinfo = from_json($self->{ua}->get($url)->decoded_content);

 my $counter=1;
 foreach my $line (@{$songinfo}) {
   $string = "$counter. '$line->{SongName}' by $line->{ArtistName} from the album '$line->{AlbumName}' at $line->{Url}.";
   $counter++;
   push(@r, $string);}

return @r;
}
